// pages/jianli/jianli.js
var App = getApp();
const chooseLocation = requirePlugin('chooseLocation');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		nickname:'',
		phone:'',
		doage:0,
		pay:0,
		dotype:0,
		area:0,
		hascard:0,
		trailcae:0,
		keep:0,
		nightword:0,
		desc:'',
		jobtype:{
			popup:false,
			text:'只干长期',
		},
		shebei:{
			popup:false,
			param:{
				activeId: 0,
				activeText:'',
			},
			component:false
		},
		region:{
		 	popup:false,
		 	text:'外省可以'
		 },
		 jobskills:{
		 	popup:false,
		 	text:[],
			data:[]
		 },
		 autosize:{
		 	minHeight:80
		 },
		 op:{
		 	text:'正反都可',
		 	value:0,
		 	popup:false
		 },
		 stype:0,
		 InitCity:{
		 	popup:false,
		 	component:false,
		 	province:'',
		 	city:'',
			latitude:0,
			longitude:0,
		 	isprovince:0,
		 	iscity:0
		 },
	},
	ShebeiPopupOpen(){
		this.setData({
			"shebei.popup":true,
			"shebei.component":true
		});
		
	},
	ShebeiChangeVal(val){
		var sid = val.detail.sid;
		var text = val.detail.text;
		this.ShebeiPopupClose();
		if(parseInt(this.data.shebei.param.activeId) != parseInt(sid)){
			this.setData({
				"shebei.param.activeId":sid,
				"shebei.param.activeText":text
			});
		}
	},
	ShebeiPopupClose(){
		this.setData({
			"shebei.popup":false
		});
	},
	ShebeiPopupAfterLeave(){
		this.setData({
			"shebei.component":false
		});
	},
	cityPopupOpen(){
		var latitude = wx.getStorageSync("latitude");
		var longitude = wx.getStorageSync("longitude");
		if(latitude&&longitude){
			this.OpentLocation(latitude,longitude)
		}else{
			wx.getLocation({
			    type: 'wgs84',
			    success :(res) => {
					this.OpentLocation(res.latitude,res.longitude)
			    },
				fail:(res) =>{
					wx.showModal({
						title: '提示',
						content: '位置请求频繁，请稍后重试！',
						showCancel:false,
						success (res) {
					    if (res.confirm) {
							// console.log('用户点击确定')
					    } else if (res.cancel) {
							// console.log('用户点击取消')
					    }
					  }
					})
				}
			})
		}
	},
	OpentLocation(latitude,longitude){
		const key = 'YBCBZ-WC4CU-P4EVZ-25TQK-UZAKE-XQBKR'; //使用在腾讯位置服务申请的key
		const referer = '75带班'; //调用插件的app的名称
		const location = JSON.stringify({
		  latitude: latitude,
		  longitude: longitude
		});
		
		wx.navigateTo({
		  url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}`
		});
	},
	ChangeCityVal(val){
		this.setData({
			"InitCity.province":val.province,
			"InitCity.city":val.city,
			"InitCity.latitude":val.latitude,
			"InitCity.longitude":val.longitude
		});
	},

	cityPopupAfterLeave(){
		this.setData({
			"InitCity.component":false
		});
	},
	
	onheaderClickLeft(){
		wx.navigateBack({})
	},

	
	jobtypePopup(){
		this.setData({
			'jobtype.popup':true
		});
	},
	onClosejobtypepopup(){
		this.setData({
			'jobtype.popup':false
		});
	},
	handlejobtypeClick(event){
		var dataset = event.target.dataset;
		this.setData({
			'dotype':dataset.val,
			'jobtype.text':dataset.text
		})
	},
	
	regionPopup(){
		this.setData({
			'region.popup':true
		});
	},
	onCloseregionpopup(){
		this.setData({
			'region.popup':false
		});
	},
	handleregionClick(event){
		var dataset = event.target.dataset;
		this.setData({
			area:dataset.val,
			'region.text':dataset.text,
		})
	},
	
	handlecertificateChange(val){
		this.setData({
			'hascard': val.detail,
		});
	},
	handleInsuranceChange(val){
		this.setData({
			'form.Insurance': val.detail,
		});
	},
	handletrailerChange(val){
		this.setData({
			'trailcae': val.detail,
		});
	},
	handlerepairChange(val){
		this.setData({
			'keep': val.detail,
		});
	},
	handleeveningChange(val){
		this.setData({
			nightword: val.detail,
		});
	},
	
	jobskillsPopup(){
		this.setData({
			'jobskills.popup':true
		});
	},
	onClosejobskillspopup(){
		this.setData({
			'jobskills.popup':false
		});
	},
	handlejobskillsClick(event){
		var dataset = event.target.dataset;
		var arr = this.data.jobskills.text;
		var farr = [];
		var text = dataset.text;
		var index = arr.indexOf(text);
		if(index>-1){
			for(var i in arr){
				if(arr[i] != text){
					farr.push(arr[i])
				}
			}
		}else{
			arr.push(text);
			for(var c in arr){
				if(arr.length>5 && c==0){
					continue;
				}
				farr.push(arr[c]);
			}
		}
		var data = this.data.jobskills.data;
		for(var d in data){
			if(farr.indexOf(data[d].text)>-1){
				data[d].checked = true;
			}else{
				data[d].checked = false;
			}
		}
		this.setData({
			'jobskills.data':data,
		});
		this.setData({
			'jobskills.text':farr,
		});
	
	},
	onOpenop(){
		this.setData({
		  'op.popup': true
		});
	},	
	onCloseop(){
		this.setData({
		  'op.popup': false
		});
	},	
	handleOpClick(event){
		var val = parseInt(event.target.dataset.val);
		var text = ''
		if(val == 2){
			text = '反手';
		}else if(val == 1){
			text = '正手';
		}else{
			text = '正反都可';
		}
		this.setData({
			'op.value': val,
			'op.text': text
		});
	},
	handleStop(){
		var self = this;
		wx.request({
			url: 'https://www.grabinter.com/Personnotes/changpersonnotesstatus',
			method: 'POST',
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			data: {
				token:wx.getStorageSync('token'),
				stype:self.data.stype?0:1
			},
			success: json => {
				if(json.data.success){
					self.setData({
						stype:self.data.stype?0:1
					});
					wx.showToast({
						title: self.data.stype?'开启成功':'关闭成功',
						icon: 'success',
						duration: 2000,
					});
				}else{
					wx.showToast({
						title: '操作失败',
						icon: 'error',
						duration: 2000,
					});
					
				}
			}
		});
	},
	GetData(){
		var self = this;
		wx.showLoading({
		  title: '加载中...',
		  mask:true
		});
		wx.request({
			url: 'https://www.grabinter.com/Personnotes/addpersonnotes',
			method: 'POST',
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			data: {
				token:wx.getStorageSync('token')
			},
			success: json => {
					var data = json.data.data;
					wx.hideLoading();
					if(!data){
						return false;
					}
					var shebeitext = '';
					if(parseInt(data.sid)>0){
						for(var i in App.globalData.shebei){
							for(var x in App.globalData.shebei[i].children){
								const itgem = App.globalData.shebei[i].children[x];
								if(itgem.sid == data.sid){
									shebeitext = itgem.text
								}
							}
						}
					}
					var optext = ''
					if(parseInt(data.op) == 2){
						optext = '反手';
					}else if(parseInt(data.op) == 1){
						optext = '正手';
					}else{
						optext = '正反都可';
					}
					var dotypetext = '';
					if(parseInt(data.dotype) == 0){
						dotypetext = '替班长期都干';
					}else if(parseInt(data.dotype) == 1){
						dotypetext = '只干替班';
					}else{
						dotypetext = '只干长期';
					}
					var regiontext = '';
					if(parseInt(data.area) == 3){
						regiontext = '只在本市干';
					}else if(parseInt(data.area) == 2){
						regiontext = '本市和周边都可以';
					}else if(parseInt(data.area) == 1){
						regiontext = '本省内都可以';
					}else{
						regiontext = '外省可以';
					}
					self.setData({
						nickname:data.nickname,
						phone:data.phone,
						doage:data.doage,
						pay:parseInt(data.pay),
						'shebei.param.activeId':data.sid,
						'shebei.param.activeText':shebeitext,
						'op.value':data.op,
						'op.text':optext,
						dotype:parseInt(data.dotype),
						'jobtype.text':dotypetext,
						area:parseInt(data.area),
						'region.text':regiontext,
						hascard:parseInt(data.hascard),
						trailcae:parseInt(data.trailcae),
						keep:parseInt(data.keep),
						nightword:parseInt(data.nightword),
						'jobskills.text':data.pskill?data.pskill.split(','):[],
						'InitCity.province':data.province,
						'InitCity.city':data.city,
						desc:data.desc,
						stype:parseInt(data.stype)
					});
					

			}
		});
	},
	formSubmit(){
		var data = this.data;
	
		if(!data.nickname){
			wx.showToast({
				title: '姓名不能为空',
				icon: 'error',
				duration: 2000
			})
			return false;
		}
		if(data.phone){
			var status = App.checkPhone(data.phone);
			if(!status){
				wx.showToast({
					title: '手机号格式错误',
					icon: 'error',
					duration: 2000
				})
				return false;
			}
		}else{
			wx.showToast({
				title: '手机号不能为空',
				icon: 'error',
				duration: 2000,
			});
			return false;
		}
		if(!data.InitCity.province || !data.InitCity.city){
			wx.showToast({
				title: '请填写期望地点',
				icon: 'error',
				duration: 2000
			})
			return false;
		}
		wx.showLoading({
		  title: '提交中...',
		  mask:true
		});
		var param = {
			token:wx.getStorageSync('token'),
			edit:true,
			nickname:data.nickname,
			phone:data.phone,
			doage:data.doage,
			pay:data.pay,
			sid:data.shebei.param.activeId || 0,
			op:data.op.value,
			dotype:data.dotype,
			province:data.InitCity.province,
			city:data.InitCity.city,
			latitude:data.InitCity.latitude,
			longitude:data.InitCity.longitude,
			area:data.area,
			hascard:data.hascard,
			trailcae:data.trailcae,
			keep:data.keep,
			nightword:data.nightword,
			pskill:data.jobskills.text.join(','),
			desc:data.desc
		};
		
		wx.request({
			url: 'https://www.grabinter.com/Personnotes/addpersonnotes',
			method: 'POST',
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			data: param,
			success: json => {
				if(json.data.success){
					wx.showToast({
						title: '提交成功',
						icon: 'success',
						duration: 2000,
					});
				}else{
					wx.showToast({
						title: '提交失败',
						icon: 'error',
						duration: 2000,
					});
					
				}
			}
		});
		
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.GetData();
		this.setData({
			'jobskills.data':App.globalData.jobskills
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		
		const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
		if(location){
			var fprovince = '';
			var fcity = '';
			if(location.province.indexOf('市')>-1){
				fprovince = location.province.split("市").join("");
			}
			if(location.province.indexOf('省')>-1){
				fprovince = location.province.split("省").join("");
			}
			if(location.city.indexOf('市')>-1){
				fcity = location.city.split("市").join("");
			}
			this.ChangeCityVal({
				province:fprovince,
				city:fcity,
				latitude:location.latitude,
				longitude:location.longitude,
			});
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
